//多选下拉框
function multiselect(selectId,selist,liNum,functions){	
	
//selectId为div下拉框的id,selist为下拉选项的数组，liNum为选中项索引
	
	var selectId = "#"+selectId;
	
	
	var selectWid=$(selectId).width()-8;
	//selectWid=parseInt(selectWid.replace(/[^0-9]/ig,""));
	//console.log(selectWid);
		
	
	//初始化下拉框选项
	this.loadList=function(listArr){
		
		var key;		
		var selectli="";		
		$(selectId).find("input").val("").change();
		$(selectId).find("ul").remove();	
		
		if(listArr=="" || listArr==undefined){
			return;
		}else{
			var indexLi="";
			for(key in listArr){
				var key2=key.trim();
				if(key=="-1"){
					indexLi+="<li><span>"+key2+"</span>"+"<font>"+listArr[key]+"</font></li>";
				}else{
					selectli+="<li><span>"+key2+"</span>"+"<font>"+listArr[key]+"</font></li>";
				}						
			}
			selectli=indexLi+selectli;
			
			$(selectId).append("<ul>"+selectli+"</ul>");  //添加选项
			
			$(selectId).find("ul").css("min-width",selectWid);

			var litotal=$(selectId).find("li");
			if(litotal.length>7){//通过判断li的个数来修改是否滚动
				$(selectId).find("ul").css("overflow-y","auto");
			}
		
		}
		
	}
	
	this.loadList(selist);
	
/*	$(selectId).find("input").click(function(){
		$(selectId).find("ul").css("display","block");
			
	})	*/
	
	this.clickfunction=function(){
			$(selectId).find("li").click(function(){//多选点击事件
				var liclass=$(this).attr("class");
				var licont=$(this).find("font").text();
				var inputcont=$(selectId).find("input").val();
				if(liclass==""||liclass==undefined){
					$(this).attr("class","selectli");
					if(inputcont==""){
					inputcont=licont;
					}else{
						inputcont=inputcont+","+licont;
					}
					$(selectId).find("input").val(inputcont).change();
				}else{
					$(this).attr("class","");
					var selectall=$(selectId).find(".selectli");
					inputcont="";	
					var l=selectall.length-1;
					for(var i=0;i<selectall.length-1;i++){	
						inputcont=inputcont+$(selectall[i]).find("font").text()+",";			
					}
					inputcont=inputcont+$(selectall[l]).find("font").text();
					$(selectId).find("input").val(inputcont).change();
				}
				
			})	
			$(selectId).find("input").keyup(function(){//多选输入框事件
				var inputcont=$(this).val().split(",");
				var licont=$(selectId).find("li");
				$(selectId).find(".selectli").attr("class","");
				for(var i=0;i<licont.length;i++){
					for(var j=0;j<inputcont.length;j++){
						if(inputcont[j]==$(licont[i]).find("font").text()){
							$(licont[i]).attr("class","selectli");
						}
					}
					
					
				}			
			})	
	}
	
	this.clickfunction();
	
	
	//初始化选中项
	
	this.loadCheck=function(){
		if(liNum==undefined){
			$(selectId).find(".selectli").attr("class","");
			$(selectId).find("input").val("").change();		
		}else{
			var licont=$(selectId).find("li");
			$(licont[liNum]).attr("class","selectli");
			var setLicont=$(selectId).find(".selectli").find("font").text();
			$(selectId).find("input").val(setLicont).change();
		}		
		
	}

	this.loadCheck();
		
	
	$("body").click(function(){
		$(".multiselect").find("ul").css("display","none");
	})	
	
	$(selectId).click(function(e){	
		if($(this).find("input").hasClass("onlyRead")){
			return;
		}else{
			$(".multiselect").find("ul").css("display","none");
			$(this).find("ul").css("display","block");
			e = e||window.event;
			e.stopPropagation();
		}		
	})	
	
	this.selectId=selectId;
	
	this.functions=functions;
}

//清空选中项
multiselect.prototype.clearCheck=function(){
	$(this.selectId).find("input").val("").change();
	$(this.selectId).find(".selectli").attr("class","");	
}


//重设下拉框选项
multiselect.prototype.reloadList=function(setList){
	this.loadList(setList);	
	this.loadCheck();
	this.clickfunction();
}

//清空下拉控件内容
multiselect.prototype.clearAll=function(){
	$(this.selectId).find("input").val("").change();
	$(this.selectId).find("ul").html("");
	
}

//返回选中项的key值(多选)
multiselect.prototype.getKeyArr=function(){
	var selectli=$(this.selectId).find(".selectli");
	var selectKey="";
	for(var i=0;i<selectli.length;i++){
		if(i==selectli.length-1){
			selectKey+=$(selectli[i]).find("span").text();
		}else{
			selectKey+=$(selectli[i]).find("span").text()+',';
		}		
	}
	return selectKey;	
}

//返回选中项的val值(多选)
multiselect.prototype.getValueArr=function(){	
	var selectli=$(this.selectId).find(".selectli");
	var selectVal="";
	for(var i=0;i<selectli.length;i++){
		if(i==selectli.length-1){
			selectVal+=$(selectli[i]).find("font").text();
		}else{
			selectVal+=$(selectli[i]).find("font").text()+',';
		}			
	}
	return selectVal;	
}

//通过key值设置选中项(多选)
multiselect.prototype.setKeyArr=function(key){
	if(key=="" ||key==undefined||key.length==0){
		return;
	}else{
		$(this.selectId).find("input").val("").change();
		$(this.selectId).find(".selectli").attr("class","");
		var allKey=$(this.selectId).find("span");
		var liList=$(this.selectId).find("li");
		for(var h=0;h<key.length;h++){
			key[h]=""+key[h];
			key[h]=key[h].trim();
			for(var i=0;i<allKey.length;i++){
				if($(allKey[i]).text()==key[h]){
					$(liList[i]).attr("class","selectli");					
				}
			}
		}
		var selectall=$(this.selectId).find(".selectli");
		var inputcont="";	
		var l=selectall.length-1;
		for(var i=0;i<selectall.length-1;i++){	
			inputcont=inputcont+$(selectall[i]).find("font").text()+",";			
		}
		inputcont=inputcont+$(selectall[l]).find("font").text();
		$(this.selectId).find("input").val(inputcont).change();
	}
}

//设置下拉框只读
multiselect.prototype.setSelectFalse=function(readonly){
	if(readonly==true){
		$(this.selectId).find("input").addClass("onlyRead");
		$(this.selectId).find("input").attr("readonly","readonly");	
	}
	else{
		$(this.selectId).find("input").removeClass("onlyRead");
		$(this.selectId).find("input").attr("readonly","");
	}
}